 /*
 * Created:   Fri Mar 17 2023
 * Processor: PIC16F877A
 * Compiler:  CCS for PIC
 * Creator:   Joshua J Port
 * Student:   2114468
 */

#include <18F452.h>						 				
#include <main.h>
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1,errors)		//Set RS232 Values for interupts

												//Global Variables
void control(void);										//Create a subroutine named control
void timer(void);										//Create a subroutine named timer
void trafficlight(void);									//Create a subroutine named trafficlight
void trafficL1(void);										//Create a subroutine named trafficL1
void trafficL2(void);										//Create a subroutine named trafficL2
void POTTEST(void);										//Create a subroutine named POTTEST
void read_pot(void);										//Create a subroutine named read_pot	
void traffic_pot(void);										//Create a subroutine named traffic_pot

												//Unsigned Integers
unsigned int change = 0;									//Create an unsigned int called change and equal it to 0
unsigned int lite = 0;
unsigned int t = 0;										//Create an unsigned int called t and equal it to 0
unsigned int u = 0;										//Create an unsigned int called t and equal it to 0
unsigned int w = 0;										//Create an unsigned int called t and equal it to 0
unsigned int x = 0;										//Create an unsigned int called t and equal it to 0
unsigned int y = 0;										//Create an unsigned int called y and equal it to 0
unsigned int z = 0;										//Create an unsigned int called t and equal it to 0
unsigned int but;										//Create an unsigned int called but  
unsigned int counter = 0;									//Assign count as an unsigned int and set the value to 0
unsigned int nums[10] ={0x6F, 0x7F, 0x07, 0x7D, 0x6D, 0x66, 0x4F, 0x5B, 0x06, 0x3F};		//Create an array from 9-0 and list it as nums
unsigned int pot0 = 0;										//Create an unsigned int called pot0 and equal it to 0
unsigned int pot1 = 0;										//Create an unsigned int called pot1 and equal it to 0
unsigned int pot2 = 0;										//Create an unsigned int called pot2 and equal it to 0
unsigned int pot3 = 0;										//Create an unsigned int called pot3 and equal it to 0

//----------------------------------------------------------------------------------------------------------------------------------------------------

#INT_TIMER0
void  TIMER0_isr(void) 						//Set the timer interupt 
{

   if(input(PIN_E2))						//If PIN_E2 is high run the statement 
      {
	 change=1;						//Set change to equal 1
      }
   read_pot();							//Read the pot value       
}

void main (void)						//Run the main loop 
   {
   
	enable_interrupts(INT_TIMER0);				//Enable interupts in the main loop
	enable_interrupts(GLOBAL);				//Enable the global interupts 
	setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128|RTCC_8_BIT);	//6.5 ms overflow
	setup_adc_ports(AN0_AN1_AN2_AN3_AN4);			//Set analogue ports as AN0, AN1, AN2, AN3, AN4
	setup_adc(ADC_CLOCK_INTERNAL);				//Set the adc device with the specified internal clock speed 
	
	

   output_high(PIN_D1);						//Turn red crossing light on   
   
 
   while(TRUE)							//Start while loop
      {
	 
	 if(change==1)						//If input E2 (BUT_1) is high - run the if loop
	    {
	       timer();						//Run timer subroutine
	    }
	    output_high(PIN_C2);
	    output_high(PIN_C5);
	    delay_ms(100);					//Delay by 1 second (1000ms)
	 if(pot0 < pot3)	
	    {
	       trafficL2();					//Run trafficL1 subroutine
	       lite=2;
	    }
	 if(change==1)						//If input E2 (BUT_1) is high - run the if loop
	    {
	       timer();						//Run timer subroutine
	    }	 
	 if(pot3 < pot0)	
	    {
	       trafficL1();					//Run trafficL2 subroutine 
	       lite=1;
	    }
      }
   }   
 
//----------------------------------------------------------------------------------------------------------------------------------------------------
 
void timer()							//Create the subroutine of timer
   {
      output_low(PIN_D1);					//Turn red crossing light off 
      output_high(PIN_D0);					//Turn green crossing light on

      while(t<=6)						//Runs the digits 1 and 1 for 6 rounds - roughly 1 second 
	 {
	    output_low(SELECT_1);				//Turn on the first segment of the 7 segment display 
	    output_high(SELECT_2);				//Turn off the second segment of the 7 segment display 
	    output_b(0x06);					//Display figure 1 on the first segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    output_high(SELECT_1);				//Turn off the first segment of the 7 segment display
	    output_low(SELECT_2);				//Turn on the second segment of the 7 segmet display 
	    output_b(0x6D);					//Display figure 1 on the second segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    t++;						//Add one to the y value 
	 }
      while(u<=6)						//Runs the digits 1 and 1 for 6 rounds - roughly 1 second 
	 {
	    output_low(SELECT_1);				//Turn on the first segment of the 7 segment display 
	    output_high(SELECT_2);				//Turn off the second segment of the 7 segment display 
	    output_b(0x06);					//Display figure 1 on the first segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    output_high(SELECT_1);				//Turn off the first segment of the 7 segment display
	    output_low(SELECT_2);				//Turn on the second segment of the 7 segmet display 
	    output_b(0x66);					//Display figure 4 on the second segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    u++;						//Add one to the y value 
	 }
      while(w<=6)						//Runs the digits 1 and 1 for 6 rounds - roughly 1 second 
	 {
	    output_low(SELECT_1);				//Turn on the first segment of the 7 segment display 
	    output_high(SELECT_2);				//Turn off the second segment of the 7 segment display 
	    output_b(0x06);					//Display figure 1 on the first segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    output_high(SELECT_1);				//Turn off the first segment of the 7 segment display
	    output_low(SELECT_2);				//Turn on the second segment of the 7 segmet display 
	    output_b(0x4F);					//Display figure 3 on the second segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    w++;						//Add one to the y value 
	 }      
      while(x<=6)						//Runs the digits 1 and 1 for 6 rounds - roughly 1 second 
	 {
	    output_low(SELECT_1);				//Turn on the first segment of the 7 segment display 
	    output_high(SELECT_2);				//Turn off the second segment of the 7 segment display 
	    output_b(0x06);					//Display figure 1 on the first segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    output_high(SELECT_1);				//Turn off the first segment of the 7 segment display
	    output_low(SELECT_2);				//Turn on the second segment of the 7 segmet display 
	    output_b(0x5B);					//Display figure 2 on the second segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    x++;						//Add one to the y value 
	 }
      while(y<=6)						//Runs the digits 1 and 1 for 6 rounds - roughly 1 second 
	 {
	    output_low(SELECT_1);				//Turn on the first segment of the 7 segment display 
	    output_high(SELECT_2);				//Turn off the second segment of the 7 segment display 
	    output_b(0x06);					//Display figure 1 on the first segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    output_high(SELECT_1);				//Turn off the first segment of the 7 segment display
	    output_low(SELECT_2);				//Turn on the second segment of the 7 segmet display 
	    output_b(0x06);					//Display figure 1 on the second segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    y++;						//Add one to the y value 
	 }            
      while(z<=6)						//Runs the digits 1 and 0 for 6 rounds - roughly 1 second 
	 {
	    output_low(SELECT_1);				//Turn on the first segment of the 7 segment display 
	    output_high(SELECT_2);				//Turn off the second segment of the 7 segment display 
	    output_b(0x06);					//Display figure 1 on the first segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    output_high(SELECT_1);				//Turn off the first segment of the 7 segment display
	    output_low(SELECT_2);				//Turn on the second segment of the 7 segmet display 
	    output_b(0x3F);					//Display figure 0 on the second segment of the 7 segment display 
	    delay_ms(10);					//Delay by 10ms
	    z++;						//Add one to the z value 
	 }
      while(counter<10)						//Run the while loop if counter value = <10
	 {
	    output_b(nums[counter]);				//Outputs to port b the corresponding hex digit with the counter number 0-9
	    delay_ms(500);					//Delay by 150ms between number changes
	    counter++;						//Add one to the counter value
	    y++;						//Add one to the y value 
	 }
      output_high(SELECT_1);					//Turn off the first segments of the 7 segment display 
      output_high(SELECT_2);					//Turn off the second segment of the 7 segment display 
      output_high(PIN_D1);					//Turn red crossing light on 
      output_low(PIN_D0);					//Turn green crossing light off
      u = 0;
      w = 0;
      x = 0;
      y = 0;							//Set Y back to value 0
      z = 0;
      counter = 0;						//Set counter to value 0
      change = 0;
   }
	    
 //----------------------------------------------------------------------------------------------------------------------------------------------------
 
void trafficlight()						//Create the subroutine of trafficlight
   {
      while(change==0)						//Start while loop 
      {
	 output_high(PIN_C2);					//Set traffic light red_1 on
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 on
	 output_low(PIN_C6);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	
	 delay_ms(100);
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 on
	 output_high(PIN_E0);					//Set traffic light orange_2 on
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(100);
	
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_low(PIN_C5);					//Set traffic light red_2 off
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_high(PIN_E1);					//Set traffic light green_2 on
	 
	 delay_ms(500);					//2000
								
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_low(PIN_C5);					//Set traffic light red_2 off
	 output_high(PIN_E0);					//Set traffic light orange_2 on
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(100);
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 on
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(100);
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_high(PIN_C3);					//Set traffic light orange_1 on 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 on
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(100);
	 
	 output_low(PIN_C2);					//Set traffic light red_1 off 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_high(PIN_C4);					//Set traffic light green_1 on
	 output_high(PIN_C5);					//Set traffic light red_2 on
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(500);					//2000
	 
	 output_low(PIN_C2);					//Set traffic light red_1 off 
	 output_high(PIN_C3);					//Set traffic light orange_1 on  
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 on
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(100);	 

	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 on
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(100);		 
      }
   }
 
 //----------------------------------------------------------------------------------------------------------------------------------------------------
 
 void trafficL1()						//Create the subroutine of trafficL1
   {
      while(lite==1)						//Run the while routine when change is equal to 1 
	 {
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 off
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(2000);
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_high(PIN_C3);					//Set traffic light orange_1 on 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 off
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(1000);
	 
	 output_low(PIN_C2);					//Set traffic light red_1 off 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_high(PIN_C4);					//Set traffic light green_1 on
	 output_high(PIN_C5);					//Set traffic light red_2 off
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(2000);
	 
	 output_low(PIN_C2);					//Set traffic light red_1 on 
	 output_high(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 off
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(1000);
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 off
	 output_low(PIN_E0);					//Set traffic light orange_2 off
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 lite=2;
	 
	 }
   }
   
//----------------------------------------------------------------------------------------------------------------------------------------------------  
   
void trafficL2()						//Create the subroutine for trafficL2
   {
      while(lite==2)						//Run the while routine when change is equal to 2
	 {
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 on 
	 output_low(PIN_E0);					//Set traffic light orange_2 off 
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(2000);
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 on 
	 output_high(PIN_E0);					//Set traffic light orange_2 on 
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(1000);
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_low(PIN_C5);					//Set traffic light red_2 off 
	 output_low(PIN_E0);					//Set traffic light orange_2 off 
	 output_high(PIN_E1);					//Set traffic light green_2 on
	 
	 delay_ms(2000);
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_low(PIN_C5);					//Set traffic light red_2 on 
	 output_high(PIN_E0);					//Set traffic light orange_2 off 
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 delay_ms(1000);
	 
	 output_high(PIN_C2);					//Set traffic light red_1 on 
	 output_low(PIN_C3);					//Set traffic light orange_1 off 
	 output_low(PIN_C4);					//Set traffic light green_1 off
	 output_high(PIN_C5);					//Set traffic light red_2 on 
	 output_low(PIN_E0);					//Set traffic light orange_2 off 
	 output_low(PIN_E1);					//Set traffic light green_2 off
	 
	 lite=0;
	 
	 }
   }
   
   
//----------------------------------------------------------------------------------------------------------------------------------------------------   


void read_pot()							//Create the subroutine of read_pot
   {
      set_adc_channel(0);					//Set the adc channel to 0 
      delay_us(1000);						//Delay by 20 micro seconds
      pot0 = read_adc();					//Assign read_adc to variable called pot0
      printf(" pot_0  = %U\n\r",pot0);				//Print pot0 value on a new line on the virtual terminal
      set_adc_channel(1);					//Set the adc channel to 0 
      delay_us(1000);						//Delay by 20 micro seconds
      pot1 = read_adc();					//Assign read_adc to variable called pot1
      printf(" pot_1  = %U\n\r",pot1);				//Print pot1 value on a new line on the virtual terminal
      set_adc_channel(2);					//Set the adc channel to 0 
      delay_us(1000);						//Delay by 20 micro seconds
      pot2 = read_adc();					//Assign read_adc to variable called pot2
      printf(" pot_2  = %U\n\r",pot2);				//Print pot2 value on a new line on the virtual terminal
      set_adc_channel(4);					//Set the adc channel to 0 
      delay_us(1000);						//Delay by 20 micro seconds
      pot3 = read_adc();					//Assign read_adc to variable called pot3
      printf(" pot_3  = %U\n\r",pot3); 				//Print pot3 value on a new line on the virtual terminal
      delay_us(1000);
   }
      
//----------------------------------------------------------------------------------------------------------------------------------------------------  

//void traffic_pot()
//	 {
//	    if(pot0 < pot3)
//	       {
//	       trafficL2();
//	       }
//	}